package Arduino;

import Charts.ChartScatter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * User: Mike
 * Date: 7/28/12
 */

// todo:    create a gui where i can pause data collection & disconnect from the arduino so I can test other code
// todo:    pausing data collection would also be good for being able to zoom without updates

public class Hygrometer implements Measurement {

    private List<Double> temperature = new ArrayList<Double>();
    private List<Double> humidity = new ArrayList<Double>();
    private List<Double> time = new ArrayList<Double>();
    private List<String> columnNames = new ArrayList<String>();

    private ChartScatter chartScatter;
    private ReadSensor readSensor;
    private WriteDataToFile dataToFile;

    private long MILLI_TO_MINUTE = 1000 * 60;
    private long MAX_UPDATE_FREQUENCY = 5 * MILLI_TO_MINUTE;
//    private long MAX_UPDATE_FREQUENCY = 5000;

    //    private final String FILE_PATH = "C:\\WhiskeyData\\output.txt";
    private final String FILE_PATH = "G:\\A_Shared_Data_Storage\\hygrometerOutput.txt";


    public Hygrometer() {
        readSensor = new ReadSensor(this, MAX_UPDATE_FREQUENCY);
        readSensor.initialize();
        createChart();
        dataToFile = new WriteDataToFile(FILE_PATH);
    }

    private void createChart() {
        String title = "Temperature & Humidity";
        String xAxis = "time";
        String yAxisL = "temperature [C]";
        String yAxisR = "humidity [%]";
        chartScatter = new ChartScatter(title, title, xAxis, yAxisL, yAxisR);
    }

    private void updateChart() {
        chartScatter.updateChart(time, temperature, humidity, "temperature [C]", "humidity [%]");
        chartScatter.setVisible(true);
//                chartScatter.displayChart(chartScatter);
    }

    public void update() {
        parseIncomingData();
        updateChart();
        writeDataToFile();
    }

    private void writeDataToFile() {
        // make sure there's data to write
        if (time.size() > 0 && temperature.size() > 0 && humidity.size() > 0) {

            // set column names first time around
            if (columnNames.size() == 0) {
                columnNames.add("milliTime");
                columnNames.add("temperature [C]");
                columnNames.add("humidity [%]");
            }

            // set the data
            List<Double> data = new ArrayList<Double>();
            data.add(time.get(time.size() - 1));
            data.add(temperature.get(temperature.size() - 1));
            data.add(humidity.get(humidity.size() - 1));

            // write it out
            try {
                dataToFile.WriteData(data, columnNames);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void parseIncomingData() {
        boolean clearReadSensor = false;
        boolean foundBeginning = false;
        boolean foundDelim = false;
        boolean foundEnd = false;

//        String end = "";
        String delim = "";
        int beggingIndex = 0;
        int delimIndex;
        int endIndex = 0;

        while (endIndex < readSensor.getLastChunk().length() - 1) {

            while (beggingIndex < readSensor.getLastChunk().length() && !delim.equals("=")) {
                delim = String.valueOf(readSensor.getLastChunk().charAt(beggingIndex));
                beggingIndex++;
                if (delim.equals("="))
                    foundBeginning = true;
            }
            delimIndex = beggingIndex;
            endIndex = beggingIndex;
            while (delimIndex < readSensor.getLastChunk().length() && !delim.equals("|")) {
                delim = String.valueOf(readSensor.getLastChunk().charAt(delimIndex));
                delimIndex++;
                if (delim.equals("|"))
                    foundDelim = true;
            }
            while (endIndex < readSensor.getLastChunk().length() && !delim.equals("-")) {
                delim = String.valueOf(readSensor.getLastChunk().charAt(endIndex));
                endIndex++;
                if (delim.equals("-"))
                    foundEnd = true;
            }
            if (endIndex > delimIndex && delimIndex > beggingIndex && foundBeginning && foundDelim && foundEnd) {
                String tempStr = "";
                for (int i = beggingIndex; i < delimIndex - 1; i++) {
                    tempStr = tempStr + String.valueOf(readSensor.getLastChunk().charAt(i));
                }
                double temp = Double.valueOf(tempStr);
                String humidStr = "";
                for (int i = delimIndex; i < endIndex - 1; i++) {
                    humidStr = humidStr + String.valueOf(readSensor.getLastChunk().charAt(i));
                }
                double humid = Double.valueOf(humidStr);
                temperature.add(temp);
                humidity.add(humid);
                time.add((double) System.currentTimeMillis());
                System.out.println(System.currentTimeMillis() + " " + temp + " " + humid);
                clearReadSensor = true;
            }
            beggingIndex++;
//            delimIndex++;
            endIndex++;
            foundBeginning = false;
            foundDelim = false;
            foundEnd = false;
        }
        if (clearReadSensor)
            readSensor.setLastChunk("");
    }

    public static void main(String[] args) throws Exception {
        Measurement hygrometer = new Hygrometer();
        System.out.println("started to hygrometer read");
    }
}